<?php

/**
 * Created by PhpStorm.
 * User: 范志俊
 * Date: 2015/4/2 0002
 * Time: 19:46
 * 作用：文章Model
 */
class Word_model extends CI_Model
{

    var $Id = '';
    var $title = '';
    var $datetime = '';
    var $label_id = '';
    var $text = '';

    function __construct()
    {
        parent::__construct();
    }

    //显示文章列表
    function show_word_list($page = 0, $limit = 10)
    {
        $this->load->library('typography');
        $this->load->helper('text');
        $this->db->cache_on();
        $query = $this->db->query('select word.*,label_list.label_name from word left JOIN label_list on label_list.label_id = word.label_id ORDER BY word.datetime DESC,Id DESC limit ' . ($page * $limit) . ',' . $limit);
        $data = $query->result_array();
        $i = 0;
        foreach ($data as $list) {
            $data[$i]['Id'] = $list['Id'];
            $data[$i]['title'] = $list['title'];
            $data[$i]['datetime'] = $list['datetime'];
            $data[$i]['label_id'] = $list['label_id'];
            $data[$i]['label_name'] = $list['label_name'];
            $data[$i]['text'] = $this->typography->auto_typography(word_limiter($list['text'], 4));
            ++$i;
        }
        return array(
            'total' => $this->db->count_all_results('word'),
            'data_num' => count($data),
            'data' => $data,
        );
    }

    //显示文章详细信息
    function show_word_info($Id = 0)
    {
        if ($Id == 0) {
            $Id_array = $this->db->select_min('Id')->from('word')->get()->row_array();
            $Id = $Id_array['Id'];
        }
        //echo $this->db->last_query();
        //$this->db->cache_on();
        $query = $this->db->query('select word.*,label_list.label_name from word left JOIN label_list on label_list.label_id = word.label_id where word.Id=' . $Id);
        $data['word_info'] = $query->row_array();
        $last = $this->db->select('Id,title')->from('word')->where('Id <', $data['word_info']['Id'])->order_by('Id desc')->get()->row_array();
        //echo $this->db->last_query()."<br>";
        $next = $this->db->select('Id,title')->from('word')->where('Id >', $data['word_info']['Id'])->order_by('Id asc')->get()->row_array();
        //echo $this->db->last_query()."<br>";
        if (empty($last)) {
            $data['last'] = array(
                'Id' => '',
                'title' => '已经是第一页',
                'class' => 'previous disabled'
            );
        } else {
            $data['last'] = array(
                'Id' => constant("FCPATH") . "/Word/word_list/" . $last['Id'],
                'title' => $last['title'],
                'class' => 'previous'
            );
        }
        if (empty($next)) {
            $data['next'] = array(
                'Id' => '',
                'title' => '已经是最后一页',
                'class' => 'next disabled'
            );
        } else {
            $data['next'] = array(
                'Id' => constant("FCPATH") . "/Word/word_list/" . $next['Id'],
                'title' => $next['title'],
                'class' => 'next'
            );
        }
        return $data;
    }

    //按住标签id显示文章
    function  label_for_word_info($label_id, $word_id)
    {
        if ($word_id != 0) {
            $query = $this->db->query('select word.*,label_list.label_name from word left JOIN label_list on label_list.label_id = word.label_id where word.Id=' . $word_id);
        } else {
            $query = $this->db->query('select word.*,label_list.label_name from word left JOIN label_list on label_list.label_id = word.label_id where word.label_id=' . $label_id);
            //echo $this->db->last_query();
        }
        $data['word_info'] = $query->row_array();
        //var_dump($data['word_info']);
        if (isset($data['word_info']['Id'])) {
            $last = $this->db->select('Id,title')->from('word')->where(array('label_id' => $label_id, 'Id <' => $data['word_info']['Id']))->order_by('Id desc')->limit(1)->get()->row_array();
            //echo $this->db->last_query()."<br>";
            $next = $this->db->select('Id,title')->from('word')->where(array('label_id' => $label_id, 'Id >' => $data['word_info']['Id']))->order_by('Id asc')->limit(1)->get()->row_array();
            //echo $this->db->last_query()."<br>";
            if (empty($last)) {
                $data['last'] = array(
                    'Id' => '',
                    'title' => '已经是第一页',
                    'class' => 'previous disabled'
                );
            } else {
                $data['last'] = array(
                    'Id' => constant("FCPATH") . "/label/label_for_word/" . $label_id . "/" . $last['Id'],
                    'title' => $last['title'],
                    'class' => 'previous'
                );
            }
            if (empty($next)) {
                $data['next'] = array(
                    'Id' => '',
                    'title' => '已经是最后一页',
                    'class' => 'next disabled'
                );
            } else {
                $data['next'] = array(
                    'Id' => constant("FCPATH") . "/label/label_for_word/" . $label_id . "/" . $next['Id'],
                    'title' => $next['title'],
                    'class' => 'next'
                );
            }
        } else {
            $data['word_info'] = array(
                'Id' => '',
                'title' => '暂无相关文章',
                'datetime' => '',
                'label_name' => '',
                'label_id' => '',
                'text' => ''
            );
            $data['last'] = array(
                'Id' => '',
                'title' => '已经是第一页',
                'class' => 'previous disabled'
            );
            $data['next'] = array(
                'Id' => '',
                'title' => '已经是最后一页',
                'class' => 'next disabled'
            );
        }
        return $data;
    }

    /*删除文章*/
    /*删除文章记录失败，日志信息添加功能 2015-5-1*/
    function delete_word($id)
    {
        $bool = $this->db->delete('word', array('Id' => $id));
        if ($bool) {
            //更新成功
            return 1;
            //return $this->db->last_query();
        } else {
            //数据异常
            $system_blog = array(
                'text' => '删除文章，记录失败。文章id为' . $Id,
                'date' => date('Y-m-d')
            );
            $this->db->insert('system_blog', $system_blog);
            return -1;
        }
    }
    /*更新文章*/
    /*更新文章记录失败，日志信息添加功能 2015-5-1*/
    function update_word($id, $title, $label_id, $text)
    {
        $data = array(
            'title' => $title,
            'label_id' => $label_id,
            'text' => $text,
            'datetime' => date("Y-m-d H:i:s")
        );
        $bool = $this->db->update('word', $data, array('Id' => $id));
        if ($bool) {
            //更新成功
            return 1;
        } else {
            //数据异常
            $system_blog = array(
                'text' => '更新文章，记录失败。更新的文章id为' . $id . ',更新文章标题为' . $title . ',更新文章标签id为' . $label_id . ',更新文章内容为' . $text . ',更新文章时间为' . $data['datetime'] . ',sql:' . $this->db->last_query(),
                'date' => date('Y-m-d')
            );
            $this->db->insert('system_blog', $system_blog);
            return -1;
        }
    }

    /*新建文章*/
    /*新建文章记录失败，日志信息添加功能 2015-5-1*/
    function insert_word($title, $label_id, $text)
    {
        $data = array(
            'title' => $title,
            'label_id' => $label_id,
            'text' => $text,
            'datetime' => date("Y-m-d H:i:s")
        );
        $bool = $this->db->insert('word', $data);
        if ($bool) {
            return $this->db->insert_id();
        } else {
            //数据库错误
            $system_blog = array(
                'text' => '新建文章，记录失败。文章标题为' . $title . ',文章标签id为' . $label_id . ',文章内容为' . $text . ',文章时间为' . $data['datetime'] . ',sql:' . $this->db->last_query(),
                'date' => date('Y-m-d')
            );
            $this->db->insert('system_blog', $system_blog);
            return -1;
        }
    }

    //搜索文章列表 2015-4-29
    function search_word_list($search)
    {
        $data = $this->db->select('Id,title')->from('word')->like('title', $search)->get()->result_array();
        if (empty($data)) {
            $system_blog = array(
                'text' => '搜索文章，搜索失败。搜索内容' . $search . ',sql:' . $this->db->last_query(),
                'date' => date('Y-m-d')
            );
            $this->db->insert('system_blog', $system_blog);
        }

        return $data;
    }

    //按照Id显示搜索到的文章
    function show_search_word($Id)
    {
        $query = $this->db->query('select word.*,label_list.label_name from word left JOIN label_list on label_list.label_id = word.label_id where word.Id=' . $Id);
        $data['word_info'] = $query->row_array();
        $data['last'] = array(
            'Id' => constant("FCPATH") . "/Word/word_list/" . $this->db->select_min('Id')->from('word')->get()->row()->Id,
            'title' => '查看第一篇文章',
            'class' => 'previous'
        );
        $data['next'] = array(
            'Id' => constant("FCPATH") . "/Word/word_list/" . $this->db->select_max('Id')->from('word')->get()->row()->Id,
            'title' => '查看最后一篇文章',
            'class' => 'next'
        );
        return $data;
    }

}